import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:craze/common/index.dart';
import 'package:craze/services/index.dart';
import 'package:craze/stores/index.dart';

class AvatarWidget extends StatelessWidget {
  final String userId;
  final String avatarUrl;
  final double radius;
  final VoidCallback? onTap;
  final bool showBorder;
  final Color borderColor;
  final double borderWidth;

  const AvatarWidget({
    Key? key,
    required this.userId,
    required this.avatarUrl,
    this.radius = 20,
    this.onTap,
    this.showBorder = false,
    this.borderColor = Colors.white,
    this.borderWidth = 2,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final AuthStore authStore = getIt<AuthStore>();

    return GestureDetector(
      onTap:
          onTap ??
          () {
            // 如果点击的是当前登录用户的头像，不进行跳转
            if (authStore.isLoggedIn &&
                authStore.currentUser != null &&
                authStore.currentUser!.id == userId) {
              return;
            }

            // 跳转到用户资料页
            context.push(AppRouter.otherProfile, extra: {'userId': userId});
          },
      child: Container(
        decoration:
            showBorder
                ? BoxDecoration(
                  shape: BoxShape.circle,
                  border: Border.all(color: borderColor, width: borderWidth),
                )
                : null,
        child: CircleAvatar(
          radius: radius.r,
          backgroundImage: AssetImage(avatarUrl),
        ),
      ),
    );
  }
}
